package com.sunstar.koobaselib.kootl.usercenter.presenter;

import com.sunstar.koobaselib.kootl.comm.base.BaseCallBack;
import com.sunstar.koobaselib.kootl.comm.tool.NetWorkTool;
import com.sunstar.koobaselib.kootl.comm.tool.RegexTool;
import com.sunstar.koobaselib.kootl.comm.tool.ToastTool;
import com.sunstar.koobaselib.kootl.usercenter.bean.BaseListBean;
import com.sunstar.koobaselib.kootl.usercenter.bean.UserLoginBean;
import com.sunstar.koobaselib.kootl.usercenter.contract.UserLoginContract;
import com.sunstar.koobaselib.kootl.usercenter.model.UserLoginModelImpl;

/**
 * Created by louisgeek on 2016/12/09
 */

public class UserLoginPresenterImpl implements UserLoginContract.Presenter<UserLoginContract.View> {


    private UserLoginContract.Model mModel;
    private UserLoginContract.View mView;

    public UserLoginPresenterImpl(UserLoginContract.View view) {
        this.attachView(view);
        mModel = new UserLoginModelImpl();
    }

    @Override
    public void attachView(UserLoginContract.View view) {
        mView = view;
    }

    @Override
    public void detachView() {
        mView = null;
    }

    @Override
    public void gainCountData(int pageCount) {
        if (!NetWorkTool.isNetWorkConnected()) {
            ToastTool.showImageWarn("无网络连接,请检查后重试!");
            return;
        }
        String username = mView.getUserName();
        String password = mView.getPassword();
        if (username == null || ("").equals(username)) {
            mView.showMessage("请输入账号");
            return;
        }
        if (!RegexTool.checkCustomRule(username, "^\\w{6,26}$")) {
            mView.showMessage("账号必须是数字字母下划线的集合！最大长度不能超过25，最小长度不能低于6");
            return;
        }
        if (password == null || ("").equals(password)) {
            mView.showMessage("请输入密码");
            return;
        }
        if (!RegexTool.checkCustomRule(username, "^\\w{6,32}$")) {
            mView.showMessage("密码必须是非空字符！最大长度不能超过32，最小长度不能低于6");
            return;
        }
        mView.showProgress();
        mModel.userLoginWeb(username, password, new BaseCallBack<BaseListBean<UserLoginBean>>() {
            @Override
            public void onSuccess(BaseListBean<UserLoginBean> data) {
                mView.hideProgress();
                if (data!=null&&data.getInfo() != null && data.getInfo().size() > 0) {
                    if ("100".equals(data.getInfo().get(0).getResult_code())) {
                        mView.loginSuccess(data.getInfo().get(0));
                    } else {
                        mView.showMessage(data.getInfo().get(0).getResult_message());
                    }
                } else {
                    mView.showMessage(data.getMessage());
                }
            }

            @Override
            public void onError(String msg) {
                mView.hideProgress();
                mView.showMessage(msg);
            }
        });
    }

    @Override
    public void gainMoreData(int pageNum) {

    }

}